package beans;

import beans.util.JsfUtil;
import entities.Personalpackage;
import controllers.PersonalpackageFacade;
import java.io.Serializable;
import java.util.List;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJBException;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.inject.Named;
import javax.inject.Inject;
import javax.faces.view.ViewScoped;

@Named(value = "personalpackageController")
@ViewScoped
public class PersonalpackageController extends AbstractController<Personalpackage> implements Serializable {

    @Inject
    private PersonalpackageFacade ejbFacade;

    private int random = FacesContext.getCurrentInstance().getCurrentInstance().hashCode();

    public int getRandom() {
        return random;
    }

    
    
    public void setRandom(int random) {
        this.random = random;
    }
    
    public PersonalpackageController() {
        super(Personalpackage.class);
    }

    @PostConstruct
    public void init() {
        super.setFacade(ejbFacade);
    }

    public void saveInvite(ActionEvent event) {
        String msg = ResourceBundle.getBundle("/Bundle").getString(itemClass.getSimpleName() + "Created");
        persist2(AbstractController.PersistAction.CREATE, msg);
        if (!isValidationFailed()) {
            items = null; // Invalidate list of items to trigger re-query.
     }
    }
    
    
    
    private void persist2(PersistAction persistAction, String successMessage) {
        if (selected != null) {
            this.setEmbeddableKeys();
            try {
                if (persistAction != PersistAction.DELETE) {
                    int prev = selected.getPPackageId();
                    System.out.println(prev + " " + random);
                    selected.setPPackageId(random);
                    selected.setModificatonCollection(null);
                        
                    System.out.println(selected.getGPackageId());
                    System.out.println(selected.getPPackageId());
                    System.out.println(selected.getUserId());
                    this.ejbFacade.create(selected);
                    selected.setPPackageId(prev);
                } else {
                    this.ejbFacade.remove(selected);
                }
                JsfUtil.addSuccessMessage(successMessage);
            } catch (EJBException ex) {
                String msg = "";
                Throwable cause = JsfUtil.getRootCause(ex.getCause());
                if (cause != null) {
                    msg = cause.getLocalizedMessage();
                }
                if (msg.length() > 0) {
                    JsfUtil.addErrorMessage(msg);
                } else {
                    JsfUtil.addErrorMessage(ex, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
                }
            } catch (Exception ex) {
                Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
                JsfUtil.addErrorMessage(ex, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
            }
        }
    }
    
    @Override
      public List<Personalpackage> getItems() {
        if (items == null) {
            String id = FacesContext.getCurrentInstance().getExternalContext().getRemoteUser();
            boolean filter = FacesContext.getCurrentInstance().getExternalContext().isUserInRole("USER");
            if(filter) items = this.ejbFacade.findByID(id);
            else items = this.ejbFacade.findAll();
        }
        return items;
    }

}
